import React, {useEffect, useState} from 'react'
import {Button, Card, Form, Input, message, Popconfirm, Select, Table, Tag} from 'antd'
import {fetchRequest} from "../../../../utils/Request";
import {url} from "../../../../utils/url";
import {useRequest} from "@umijs/hooks";
import {number} from "prop-types";
import exportExcel from "../../../../utils/exportExcel";
import TableDownloadToolbar from "../../../../components/TableSetting/TableDownloadToolbar";
import {getOriginString} from "../../../../utils/encrypt";

interface requestParamTable {
    deptId: null | undefined | string,
    startDate: null | undefined | string,
    endDate: null | undefined | string,
    pageNum: number,
    pageSize: number,
    schoolMark: number
}

interface downloadParam {
    deptId: null | undefined | string,
    startDate: null | undefined | string,
    endDate: null | undefined | string,
    schoolMark: number
}

export default function AtDormTable(props: any) {

    // console.log(props);
    const [tableData, setTableData] = useState<any>([]);
    const [requestParamTable,setRequestParamTable] = useState<any>([]);

    useEffect(() => {
        if( isNull(requestParamTable.deptId)|| isNull(requestParamTable.startDate) || isNull(requestParamTable.endDate)){
           //doNothing
        }
        else{
            // getAtDormTableRequest(requestParamTable)
        }
    }, [requestParamTable]);

    useEffect(() => {
        setRequestParamTable(props.params)
        setTableData(props.tableDatas)
    }, [props]);

    const columns = [
        {
            title: '姓名',
            dataIndex: 'studentName',
            key: 'studentName',
        },
        {
            title: '班级',
            dataIndex: 'deptName',
            key: 'deptName',
        },
        {
            title: '联系电话',
            dataIndex: 'phone',
            key: 'phone',
            render: (text: string) => getOriginString(text)
        },
    ];

    //请求在寝学生清单
    async function getAtDormTableRequest(params: requestParamTable) {
        try {
            console.log(params)
            const json = await fetchRequest(url.GET_AT_QUERY_NUMBER, params);
            if (json.success) {
                message.destroy();
                // console.log(json.data)
                setTableData( json.data.myPager);

            } else {
                message.destroy();
                message.error(json.error)
            }
        } catch (error:any) {
            message.destroy();
            message.error(error.message)

        }
    }

    //请导出请求数据清单
    async function getAtDormExcelRequest(params: downloadParam) {
        try {
            console.log(params)
            const json = await fetchRequest(url.GET_AT_DORM_EXCEL, params);
            if (json.success) {
                message.destroy();
                const atSchool = params.schoolMark == 1 ? "在寝" : "不在寝";
                json.data.map((item:any)=>{
                    item.姓名 = item.studentName;
                    item.电话 = item.phone;
                    item.电话 = getOriginString(item.电话);
                    item.组织机构 = item.deptName;
                    delete item.studentName;
                    delete item.phone;
                    delete item.deptName;
                });
                if(json.data.length==0){
                    message.info("数据为空");
                    return false;
                }
                exportExcel(json.data,atSchool+"清单导出"+getRandom(10)+".xlsx");

            } else {
                message.destroy();
                message.error(json.error)
            }
        } catch (error:any) {
            message.destroy();
            message.error(error.message)

        }
    }

    //生成随机数
    function getRandom(num:number){
        return  Math.floor((Math.random()+Math.floor(Math.random()*9+1))*Math.pow(10,num-1));
    }

    const getAtDormExcelData = useRequest(getAtDormExcelRequest, {
        loadingDelay: 200,
        manual: true
    });

    //点击分页的时候
    function handlePageChange(page: number) {
        getAtDormTableRequest({
            ...requestParamTable,
            pageNum: page
        })
    }

    function isNull(param:any) {
        return param === "" || param == undefined;
    }
    
    function onDownload() {

        if(isNull(requestParamTable.deptId) || isNull(requestParamTable.startDate) || isNull(requestParamTable.endDate)){
            message.error("请点击搜索后再导出！");
            return false;
        }

        const downLoadParam = {
            deptId : requestParamTable.deptId,
            startDate : requestParamTable.startDate,
            endDate : requestParamTable.endDate,
            schoolMark : requestParamTable.schoolMark
        };

        getAtDormExcelData.run(downLoadParam);
        // message.info("点击导出")
    }

    const dataSource = tableData.rows || [];

    return (
        <Card className="cardStyle">
            <TableDownloadToolbar tableName="统计清单" onDownload={onDownload}/>
            <Table dataSource={dataSource} columns={columns}
                   pagination={{  simple: true, current: tableData.page + 1,  pageSize: 10,onChange: handlePageChange,showSizeChanger: false, total: tableData.total }} rowKey={(record: any) => record.phone}
            />
        </Card>

     )

}